<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Component
 * @version 		$Id: index.class.php 300 2009-03-20 13:01:54Z Raymond_Benc $
 */
class Recordvideo_Component_Controller_Frame extends Phpfox_Component {

    /**
     * Class process method wnich is used to execute this component.
     */
    public function process() {
       
        $iVideoId = 0;

        if (($aVals = $this->request()->getArray('val'))) {
            $sId = $aVals['id'];
 
            require(PHPFOX_DIR . 'ex' . DIRECTORY_SEPARATOR . 'opentok' . DIRECTORY_SEPARATOR . 'OpenTokSDK.php');

            $apiObj = new OpenTokSDK(Phpfox::getParam('recordvideo.opentok_api_key'), Phpfox::getParam('recordvideo.opentok_secret'));

            // Creating an OpenTok server-enabled session
            $session = $apiObj->createSession();
            $sessionId = $session->getSessionId();
            $token = $apiObj->generate_token($sessionId, 'moderator');

            $sVideoUrl = '';
            try {
                $archiveObj = $apiObj->getArchiveManifest($sId, $token);

                if ($archiveObj->getId()) {
                    $resourceObj = $archiveObj->getResources();
                    $sVideoUrl = $archiveObj->downloadArchiveURL((string) $resourceObj[0]->getId(), $token);
                } else {
                    Phpfox_Error::set('Can not get video id');
                }
            } catch (OpenTokException $e) {
                Phpfox_Error::set($e->getMessage());
            }


            $sVideo = Phpfox::getService('recordvideo.process')->downloadVideo($sVideoUrl);

            if ($sVideo !== FALSE) {
                $sDirVideo = Phpfox::getParam('video.dir') . $sVideo;

                $iVideoId = Phpfox::getService('video.process')->add(array('title' => ''), array(
                    'name' => '',
                    'ext' => $this->_getFileExtension($sVideo)
                ));

                if ($iVideoId) {
                    //***image
//                    $sImage = '';
//                    $sDirImageBase = Phpfox::getParam('video.dir_image');
//                    $sDirImage = Phpfox::getService('recordvideo.process')->buildDir($sDirImageBase) . md5(uniqid() . PHPFOX_TIME) . '.png';
//                    if($aVals['image']) {
//                        if(imagepng($aVals['image'], $sDirImage)) {
//                            $sImage = str_replace($sDirImageBase, '', $sImages, $sDirImage);
//                        }
//                    }
                    //***#image
                    
                    Phpfox::getLib('database')->update(Phpfox::getT('video'), array(
                        'destination' => $sVideo,
                    ), 'video_id = ' . $iVideoId);
                }
            }
        }

        if ($iVideoId && Phpfox_Error::isPassed()) {
            echo '<script type="text/javascript">';
            echo 'window.parent.$.ajaxCall("video.convert", "attachment_id='.$iVideoId.'&full=true", "GET");';
            echo '</script>';
        } else {
            
            echo '<script type="text/javascript">';
            echo 'window.parent.$("#js_upload_error_message").show();';
            echo 'window.parent.$("#js_upload_error_message").html("'. implode('<br />', Phpfox_Error::get()) .'");';
            echo '</script>';
        }

        die();
    }

    private function _getFileExtension($sFileName) {
        $sFileName = strtolower($sFileName);
        $aExts = preg_split("/[\/\\.]/", $sFileName);
        $iCnt = count($aExts) - 1;

        return strtolower($aExts[$iCnt]);
    }

    /**
     * Garbage collector. Is executed after this class has completed
     * its job and the template has also been displayed.
     */
    public function clean() {
        
    }

}

?>